<?php

namespace App\Services\Game;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Models\Game\User\User;
use Illuminate\Database\Eloquent\Builder;

class UserService extends Service
{
    use DatabaseMysql;

    /**
     * 获取数据列表
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Collection[]|int[]
     */
    public function getList($request)
    {
        $query = User::query();

        // 筛选【平台】
        $this->makeWhereParamOrg($request, $query);
        // 筛选【访问控制】
        $this->makeWhereParamBits($request, 'access', $query);
        // 筛选【角色】
        $this->makeWhereParamBits($request, 'role', $query);
        // 筛选【搜索】
        $this->makeWhereParamSearch($request, $query);

        // 数据条数
        $total = (clone $query)->count();
        // 排序分页
        $this->makeOrderParam($request, $query)->makePaginationParam($request, $query);
        // 查询数据
        $collection = $query->with([
            'wallet', 'playerSlo:id,slo_model_id', 'playerFish:id,fish_model_id',
            'org:code,name', 'player:id,last_game_time,last_game_id'
        ])->get();

        return [
            $collection,
            $total
        ];
    }

    /**
     * 搜索数据列表
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function search($request)
    {
        $query = User::query();
        // 筛选【平台】
        $this->makeWhereParamOrg($request, $query);

        return $query->with('org:code,name')
            ->where('account_id', 'like', $request->input('keyword') . '%')
            ->limit($this->getPageSize())
            ->get();
    }
}
